package com.example.demo.project.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.utils.redis.RedisUtil;
import com.example.demo.project.entity.User;
import com.example.demo.project.mapper.UserMapper;
import com.example.demo.common.utils.TokenUtils;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {
    public User login(User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        User one= getOne(queryWrapper);
        if(one!=null){
            String token = TokenUtils.genToken(one);
            RedisUtil.Set("USER_TOKEN"+user.getUsername(),token);
            one.setToken(token);
        }
//        throw new ServiceCustomerException(Constants.CODE_500,"系统异常");
        return one;
    }
    public User getUser(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return getOne(queryWrapper);
    }
}
